package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.model.AutoCodePart;
import com.dk.mapper.AutoCodePartMapper;
import com.dk.service.AutoCodePartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.AutoCodePartVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 编码生成规则组成表 服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-18
 */
@Service
public class AutoCodePartServiceImpl extends ServiceImpl<AutoCodePartMapper, AutoCodePart> implements AutoCodePartService {
    @Resource
    private AutoCodePartMapper autoCodePartMapper;

    @Override
    public List<AutoCodePartVO> getInfoListByTrim(AutoCodePartVO autoCodePartVO) {
        //分段编码,分段名称 模糊查询
        LambdaQueryWrapper<AutoCodePart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AutoCodePart::getRuleId, autoCodePartVO.getRuleId());//必须传的字段
        wrapper.like(StringUtils.isNotBlank(autoCodePartVO.getPartName()), AutoCodePart::getPartName, autoCodePartVO.getPartName());//分段名称
        wrapper.like(StringUtils.isNotBlank(autoCodePartVO.getPartCode()), AutoCodePart::getPartCode, autoCodePartVO.getPartCode());//分段编码
        wrapper.orderByAsc(AutoCodePart::getPartIndex);//排序显示
        List<AutoCodePartVO> collect = autoCodePartMapper.selectList(wrapper).stream().map(autoCodePart -> {
            AutoCodePartVO vo = new AutoCodePartVO();
            BeanUtils.copyProperties(autoCodePart, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void insertInfoAutoCodePart(AutoCodePartVO autoCodePartVO) {
        AutoCodePart autoCodePart = new AutoCodePart();
        BeanUtils.copyProperties(autoCodePartVO, autoCodePart);
        autoCodePartMapper.insert(autoCodePart);
    }

    @Override
    public AutoCodePartVO getInfoAutoCodePartById(Long partId) {
        AutoCodePart autoCodePart = autoCodePartMapper.selectById(partId);
        AutoCodePartVO vo = new AutoCodePartVO();
        BeanUtils.copyProperties(autoCodePart, vo);
        return vo;
    }

    @Override
    public void updateInfoAutoCodePartById(AutoCodePartVO autoCodePartVO) {
        AutoCodePart autoCodePart = new AutoCodePart();
        BeanUtils.copyProperties(autoCodePartVO, autoCodePart);
        autoCodePartMapper.updateById(autoCodePart);
    }

    @Override
    public void deleteInfoAutoCodePartById(Long partId) {
        autoCodePartMapper.deleteById(partId);
    }
}
